<?php

class BScreenFlow extends CComponent {

    public $screenFlows = array();
    // public $screens = array();
    protected $rudeScreens = array();
    private $currentFlowId = null;
    private $importedDefaultMeta = false;

    public function __construct($flows = null) {

        if (is_string($flows))
            $this->importScreenFlow($flows);
        
        if (is_array($flows))
            $screenFlows = $flows;
    }

    public function addScreenFlow($screenFlow) {
        array_push($screenFlows, $screenFlow);
    }

    public function addScreenToFlow($screenFlow, $screen) {
        array_push($screenFlows['screens'], $screen);
    }

    public function getScreenFlow($name) {
        return $screenFlows[$name];
    }

    public function createScreen($screenName, $screenTitle, $screenView, $screenData, $attributes = array()) {
        $screenItem = array();

        if (isset($screenTitle))
            $screenItem['title'] = $screenTitle;
        if (isset($screenView))
            $screenItem['view'] = $screenView;
        if (isset($screenData))
            $screenItem['data'] = $screenData;

        $screen = array();

        if (!empty($screenItem)) {
            if (isset($screenName)) {
                $screen[$screenName] = $screenItem;
            }
            else
                array_push($screen, $screenItem);
        }

        return $screen;
    }

    public function importScreenFlow($PathAliasOfMetaFile = false) {

        if (!$PathAliasOfMetaFile) {
            $rudeScreens = require(Yii::getPathOfAlias('application.meta.screenFlows') . '.php');
            $this->importedDefaultMeta = true;
        } else {
            $rudeScreens = require(Yii::getPathOfAlias($pathAlias) . '.php');
        }

        //	$rudeScreens=require(Yii::getPathOfAlias('application.meta').'/siteMenus.php');
        foreach ($rudeScreens as $name => $screenflow) {
            $this->screenFlows[$name] = $screenflow;
        }
    }

    protected function retrieveScreensMenu($screenflowName) {

        $menu = array();
        $items = array();
        $flowId = $screenflowName;
        $screenflow = $this->screenFlows[$screenflowName];
        foreach ($screenflow['screens'] as $screenId => $screen) {
            $menuItem = array();
            $menuItem['label'] = isset($screen['linkText']) ? $screen['linkText'] : $screen['title'];
            $menuItem['url'] = "/" . $flowId . "/" . $screenId . "/";
            $menuItem['url'] = Yii::app()->getController()->getRoute() . "/" . $flowId . "/" . $screenId . "/";
            array_push($items, $menuItem);
        }

        $menu['items'] = $items;

        return $menu;
    }

    public function getBootStrapMenu($screenflowName, $fromMetaFile = true) {
        if ($fromMetaFile)
            $this->importScreenFlow();
        else if(is_string($fromMetaFile))
            $this->importScreenFlow ($fromMetaFile);
        
        $this->currentFlowId = $screenflowName;
        $menu = $this->retrieveScreensMenu($screenflowName);
        $this->setActiveFlowMenu($menu);
        $this->normalizeUrl($menu);
        return $menu;
    }

    protected function normalizeUrl(&$menu) {
        //$items = $menu['items'];
        foreach ($menu['items'] as &$item) {
            $url = array();
            if (isset($item['url'])) {
                array_push($url, $item['url']);
                $item['url'] = CHtml::normalizeUrl($url);
            }
            if (isset($item['items'])) {
                $this->normalizeUrl($item);
            }
        }

        //    return $menu;
    }

    protected function setActiveFlowMenu(&$menu) {
        $pathInfo = Yii::app()->getRequest()->getPathInfo();
        $route = Yii::app()->getController()->getRoute();
        $flowId = $this->currentFlowId;
        $currentFlow = $this->screenFlows[$this->currentFlowId];

        if ($pathInfo == $route) {
            $currentScreens = $currentFlow['screens'];
            if (isset($currentFlow['screenSequence']))
                $screenId = $currentFlow['screenSequence'][0];
            else
                $screenId = key($currentFlow->screens[0]);
        }
        else {
            list($controller, $action, $flowId, $screenId) = preg_split('[/]', $pathInfo);
        }

        foreach ($menu['items'] as &$item) {
            if (isset($item['url'])) {
                if (trim($item['url']) == $route . "/" . $flowId . "/" . $screenId . "/") {
                    $item['active'] = true;
                } else {
                    unset($item['active']);
                }
            };
        }
    }

}